package com.css.apps.base.func.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.func.model.SFunc;
import com.css.apps.base.func.model.SFuncAction;
import com.css.apps.base.log.model.LogConstant;
import com.css.apps.base.log.model.LogPart;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ModelDriven;

/**
 * 功能项添加
 * 
 * @author FangWQ
 * @version v0.1
 * @since 2014-3-5 下午03:37:46
 */
@SuppressWarnings("serial")
public class UpdFuncPoint extends AdminAction implements ModelDriven {
	private static Log log = LogFactory.getLog(UpdFuncPoint.class);
	private String sysId;
	private String funcCode;
	private String actionCode;
	private String actionName;
	private SFuncAction item;

	public UpdFuncPoint() {
		this.item = new SFuncAction();
	}

	@Override
	protected String adminGo() {
		TransactionCache tx = null;
		try {
			// 保存功能
			if (StringHelper.isEmpty(item.getActionCode()) || StringHelper.isEmpty(item.getActionName())
					|| StringHelper.isEmpty(item.getFuncCode())) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
				return Action.ERROR;
			}
			SFuncAction functemp = QueryCache.get(SFuncAction.class, item.getUuid());
			functemp.setActionCode(item.getActionCode());
			functemp.setActionName(item.getActionName());
			functemp.setOpenFlag(item.getOpenFlag());
			tx = new TransactionCache();
			tx.update(functemp);
			tx.commit();

			LogPart lp = new LogPart();
			lp.setOpObjType(SFuncAction.class.getName());
			lp.setOpObjId(item.getUuid());
			lp.setRelObjType(SFunc.class.getName());
			lp.setRelObjId(funcCode);
			lp.setOpType(LogConstant.LOG_TYPE_ADD);
			lp.setLogLevel(LogConstant.LOG_LEVEL_COMMON);
			// lp.setLogData(Json.object2json(action));
			lp.setResult(LogConstant.RESULT_SUCCESS);
			lp.save();

			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			if (tx != null) {
				tx.rollback();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getSysId() {
		return sysId;
	}

	public void setSysId(String sysId) {
		this.sysId = sysId;
	}

	public String getFuncCode() {
		return funcCode;
	}

	public void setFuncCode(String funcCode) {
		this.funcCode = funcCode;
	}

	public String getActionCode() {
		return actionCode;
	}

	public void setActionCode(String actionCode) {
		this.actionCode = actionCode;
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public SFuncAction getItem() {
		return item;
	}

	public void setItem(SFuncAction item) {
		this.item = item;
	}

	@Override
	public Object getModel() {
		return item;
	}

}
